<template>
  <a-row :gutter="20">
    <a-col :span="10">
      <a-spin :spinning="loading">
        <a-card
          title="商标默认规则"
        >
          <template slot="extra">
            <a-button type="link" icon="save" @click="onSubmit"></a-button>
          </template>
          <a-form-model
            ref="tradeForm"
            :model="tradeForm"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-form-model-item label="是否开启">
              <a-radio-group v-model="tradeForm.auto_rk_open">
                <a-radio-button :value="1">开启</a-radio-button>
                <a-radio-button :value="0">停止</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="入库上限(日）">
              <a-input v-model="tradeForm.auto_rk_day_limit" type="text" placeholder="每日入库的上限" />
            </a-form-model-item>
            <a-form-model-item label="间隔资源数">
              <a-input-number v-model="tradeForm.auto_rk_interval_num" style="width: 100%" type="text" placeholder="每次循环进入本库的条数" />
            </a-form-model-item>
            <a-form-model-item label="是否开启报警间隔">
              <a-radio-group v-model="tradeForm.auto_rk_call_interval_open">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="0">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="报警时间段">
              <a-time-picker :default-open-value="moment('00:00:00', 'HH:mm:ss')" @change="onChange1" v-model="tradeForm.auto_rk_call_interval_start_time" />
              <a-time-picker :default-open-value="moment('00:00:00', 'HH:mm:ss')" @change="onChange2" v-model="tradeForm.auto_rk_call_interval_end_time" />
            </a-form-model-item>
            <a-form-model-item label="报警间隔（分）">
              <a-input-number v-model="tradeForm.auto_rk_call_interval" style="width: 100%" type="text" placeholder="多少分钟无资源进入发送短信提醒" />
            </a-form-model-item>
            <a-form-model-item label="距离上限条数报警">
              <a-input-number v-model="tradeForm.auto_rk_call_dst" style="width: 100%" type="text" placeholder="距离上限多少条发送短信提醒" />
            </a-form-model-item>
            <a-form-model-item label="达到上限是否报警">
              <a-radio-group v-model="tradeForm.auto_rk_call_to_limit">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="2">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="接受短信的手机号">
              <a-input v-model="tradeForm.auto_rk_phone" type="text" placeholder="多个手机号用,隔开（英文）"></a-input>
              <!--                  <a-select-->
              <!--                    mode="multiple"-->
              <!--                    v-model="settingForm.auto_rk_phone"-->
              <!--                    placeholder="选择接受报警短信的手机号码（多选）"-->
              <!--                  >-->
              <!--                  </a-select>-->
            </a-form-model-item>
            <a-form-model-item label="剩余未领暂停入库（条数）">
              <a-input-number v-model="tradeForm.auto_rk_not_received_count" style="width: 100%" type="text" placeholder="未领取剩余多少条之后暂停入库" />
            </a-form-model-item>
          </a-form-model>
        </a-card>
      </a-spin>
    </a-col>
    <a-col :span="10">
      <a-spin :spinning="ptLoading">
        <a-card
          title="专利默认规则"
        >
          <template slot="extra">
            <a-button type="link" icon="save" @click="onSubmit1"></a-button>
          </template>
          <a-form-model
            ref="patentForm"
            :model="patentForm"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-form-model-item label="是否开启">
              <a-radio-group v-model="patentForm.auto_rk_zl_open">
                <a-radio-button :value="1">开启</a-radio-button>
                <a-radio-button :value="0">停止</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="入库上限(日）">
              <a-input v-model="patentForm.auto_rk_zl_day_limit" type="text" placeholder="每日入库的上限" />
            </a-form-model-item>
            <a-form-model-item label="间隔资源数">
              <a-input-number v-model="patentForm.auto_rk_zl_interval_num" style="width: 100%" type="text" placeholder="每次循环进入本库的条数" />
            </a-form-model-item>
            <a-form-model-item label="是否开启报警间隔">
              <a-radio-group v-model="patentForm.auto_rk_zl_call_interval_open">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="0">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="报警时间段">
              <a-time-picker :default-open-value="moment('00:00:00', 'HH:mm:ss')" @change="onChange3" v-model="patentForm.auto_rk_zl_call_interval_start_time" />
              <a-time-picker :default-open-value="moment('00:00:00', 'HH:mm:ss')" @change="onChange4" v-model="patentForm.auto_rk_zl_call_interval_end_time" />
            </a-form-model-item>
            <a-form-model-item label="报警间隔（分）">
              <a-input-number v-model="patentForm.auto_rk_zl_call_interval" style="width: 100%" type="text" placeholder="多少分钟无资源进入发送短信提醒" />
            </a-form-model-item>
            <a-form-model-item label="距离上限条数报警">
              <a-input-number v-model="patentForm.auto_rk_zl_call_dst" style="width: 100%" type="text" placeholder="距离上限多少条发送短信提醒" />
            </a-form-model-item>
            <a-form-model-item label="达到上限是否报警">
              <a-radio-group v-model="patentForm.auto_rk_zl_call_to_limit">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="2">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="接受短信的手机号">
              <a-input v-model="patentForm.auto_rk_zl_phone" type="text" placeholder="多个手机号用,隔开（英文）"></a-input>
              <!--                  <a-select-->
              <!--                    mode="multiple"-->
              <!--                    v-model="settingForm.auto_rk_phone"-->
              <!--                    placeholder="选择接受报警短信的手机号码（多选）"-->
              <!--                  >-->
              <!--                  </a-select>-->
            </a-form-model-item>
            <a-form-model-item label="剩余未领暂停入库（条数）">
              <a-input-number v-model="patentForm.auto_rk_zl_not_received_count" style="width: 100%" type="text" placeholder="未领取剩余多少条之后暂停入库" />
            </a-form-model-item>
          </a-form-model>
        </a-card>
      </a-spin>
    </a-col>
  </a-row>
</template>

<script>
  import moment from 'moment'
  import { getEditAuto, getEditPatent, postEditAuto, postEditPatent } from '@/api/customer'

  export default {
    name: 'Setting',
    data () {
      return {
        labelCol: { span: 8 },
        wrapperCol: { span: 14 },
        settingRules: {},
        tradeForm: {},
        patentForm: {},
        settingForm: {},
        selectedItems: [],
        loading: false,
        ptLoading: false
      }
    },
    created () {
      this.getEdit()
      this.getEditPatent()
    },
    methods: {
      moment,
      onChange1 (time, timeString) {
        // this.tradeForm['auto_rk_call_interval_start_time'] = this.moment(timeString, 'HH:mm:ss')
      },
      onChange2 (time, timeString) {
        // this.tradeForm['auto_rk_call_interval_end_time'] = this.moment(timeString, 'HH:mm:ss')
      },
      onChange3 (time, timeString) {
        // this.patentForm['auto_rk_zl_call_interval_start_time'] = this.moment(timeString, 'HH:mm:ss')
      },
      onChange4 (time, timeString) {
        // this.patentForm['auto_rk_zl_call_interval_end_time'] = this.moment(timeString, 'HH:mm:ss')
      },
      getEditPatent () {
        this.ptLoading = true
        getEditPatent().then(res => {
          if (res.status === 0) {
            this.patentForm = res.result.auto_zl_setting
            this.patentForm['auto_rk_zl_open'] = parseInt(res.result.auto_zl_setting.auto_rk_zl_open)
            this.patentForm['auto_rk_zl_call_interval_open'] = parseInt(res.result.auto_zl_setting.auto_rk_zl_call_interval_open)
            this.patentForm['auto_rk_zl_call_to_limit'] = parseInt(res.result.auto_zl_setting.auto_rk_zl_call_to_limit)
            this.patentForm['auto_rk_zl_call_interval_start_time'] = res.result.auto_zl_setting.auto_rk_zl_call_interval_start_time === '' ? null : this.moment(res.result.auto_zl_setting.auto_rk_zl_call_interval_start_time, 'HH:mm:ss')
            this.patentForm['auto_rk_zl_call_interval_end_time'] = res.result.auto_zl_setting.auto_rk_zl_call_interval_end_time === '' ? null : this.moment(res.result.auto_zl_setting.auto_rk_zl_call_interval_end_time, 'HH:mm:ss')
          }
        }).finally(() => {
          this.ptLoading = false
        })
      },
      getEdit () {
        this.loading = true
        getEditAuto().then(res => {
          if (res.status === 0) {
            this.tradeForm = res.result.auto_setting
            this.tradeForm['auto_rk_open'] = parseInt(res.result.auto_setting.auto_rk_open)
            this.tradeForm['auto_rk_call_interval_open'] = parseInt(res.result.auto_setting.auto_rk_call_interval_open)
            this.tradeForm['auto_rk_call_to_limit'] = parseInt(res.result.auto_setting.auto_rk_call_to_limit)
            this.tradeForm['auto_rk_call_interval_start_time'] = res.result.auto_setting.auto_rk_call_interval_start_time === '' ? null : this.moment(res.result.auto_setting.auto_rk_call_interval_start_time, 'HH:mm:ss')
            this.tradeForm['auto_rk_call_interval_end_time'] = res.result.auto_setting.auto_rk_call_interval_end_time === '' ? null : this.moment(res.result.auto_setting.auto_rk_call_interval_end_time, 'HH:mm:ss')
          }
        }).finally(() => {
          this.loading = false
        })
      },
      onStop (e) {
        const app = this
        app.loading = true
        const data = {}
        data['auto_setting[auto_rk_open]'] = e.target.value
        postEditAuto(data).then(res => {
          if (res.status === 0) {
            app.loading = false
            app.getEdit()
          } else {
            app.$warning({
              title: res.msg
            })
            app.loading = false
          }
        })
      },
      onStop1 (e) {
        const app = this
        app.loading = true
        const data = {}
        data['auto_zl_setting[auto_rk_zl_open]'] = e.target.value
        postEditPatent(data).then(res => {
          if (res.status === 0) {
            app.loading = false
            app.getEditPatent()
          } else {
            app.$warning({
              title: res.msg
            })
            app.loading = false
          }
        })
      },
      onSubmit () {
        const app = this
        const data = {}
        app.$refs.tradeForm.validate(valid => {
          if (valid) {
            app.loading = true
            for (const k in app.tradeForm) {
              if (k === 'auto_rk_call_interval_start_time' || k === 'auto_rk_call_interval_end_time') {
                if (app.tradeForm[k]) {
                  data['auto_setting[' + k + ']'] = app.tradeForm[k].format('HH:mm:ss')
                } else {
                  data['auto_setting[' + k + ']'] = ''
                }
              } else {
                data['auto_setting[' + k + ']'] = app.tradeForm[k]
              }
            }
            postEditAuto(data).then(res => {
              if (res.status === 0) {
                app.loading = false
              } else {
                app.$warning({
                  title: res.msg
                })
                app.loading = false
              }
            })
          }
        })
      },
      onSubmit1 () {
        const app = this
        const data = {}
        app.$refs.patentForm.validate(valid => {
          if (valid) {
            app.ptLoading = true
            for (const k in app.patentForm) {
              if (k === 'auto_rk_zl_call_interval_start_time' || k === 'auto_rk_zl_call_interval_end_time') {
                if (app.patentForm[k]) {
                  data['auto_zl_setting[' + k + ']'] = app.patentForm[k].format('HH:mm:ss')
                } else {
                  data['auto_zl_setting[' + k + ']'] = ''
                }
              } else {
                data['auto_zl_setting[' + k + ']'] = app.patentForm[k]
              }
            }
            postEditPatent(data).then(res => {
              if (res.status === 0) {
                app.ptLoading = false
              } else {
                app.$warning({
                  title: res.msg
                })
                app.ptLoading = false
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
